//
//  CurrentOrderModel.m
//  Reservation
//
//  Created by Wai Man Chan on 4/6/14.
//  Copyright (c) 2014 CHAN, Wai Man. All rights reserved.
//

#import "CurrentOrderModel.h"
#import "CurrentOrderTableViewController.h"
#import "AddCommentViewController.h"

@interface CurrentOrderModel () {
    CLLocationManager *locationManager;
    CLLocation *location;
    CurrentOrderTableViewController *orderController;
    AddCommentViewController *commentController;
}


@end

extern UIViewController *rootController;

#define UUID @"0B8381BA-9A4A-4A88-86C4-7DE03A9CC0BC"

@implementation CurrentOrderModel
- (id)init {
    self = [super init];
    _beacon = NULL;
    locationManager = [CLLocationManager new];
    CLBeaconRegion *region = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:UUID] identifier:@"Restaurant"];
    region.notifyOnEntry = YES;
    region.notifyOnExit = YES;
    region.notifyEntryStateOnDisplay = YES;
    locationManager.delegate = self;
    [locationManager startMonitoringForRegion:region];
    [locationManager startRangingBeaconsInRegion:region];
    tabBar = (UITabBarController *)rootController.parentViewController;
    orderController = [[CurrentOrderTableViewController alloc] initWithNibName:@"CurrentOrderTableViewController" bundle:[NSBundle mainBundle]];
    orderController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Current Order" image:NULL tag:4];
    commentController = [[AddCommentViewController alloc] initWithNibName:@"AddCommentViewController" bundle:[NSBundle mainBundle]];
    commentController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Current Order" image:NULL tag:5];
    return self;
}


- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
    
    beacons = [beacons filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"rssi.intValue > -60"]];
    if (beacons.count > 0) {
        [self willChangeValueForKey:@"beacon"];
        _beacon = NULL;
        for (CLBeacon *beacon in beacons) {
            if (_beacon) {
                if (_beacon.rssi > beacon.rssi) _beacon = beacon;
            }
            else _beacon = beacon;
        }
        [self didChangeValueForKey:@"beacon"];
        if (tabBar.viewControllers.count == 3) {
            NSMutableArray *array = [NSMutableArray arrayWithArray:tabBar.viewControllers];
            [array addObject:orderController];
            tabBar.viewControllers= array;
        }
    } else {
        if (tabBar.selectedViewController == orderController) {
            NSString *message = [NSString stringWithFormat:@"You left table %d too far, so I can't order for you now. Please try put me near the table", self.tableNumber.intValue];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Can't order for you now. " message:message delegate:NULL cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
            commentController.restaurantID = [self restaurantNumber].longLongValue;
            NSMutableArray *array = [NSMutableArray arrayWithArray:tabBar.viewControllers];
            [array removeObject:orderController];
            [array addObject:commentController];
            tabBar.viewControllers= array;
            tabBar.selectedIndex = 4;
        }
        _beacon = NULL;
    }
}

- (void)locationManager:(CLLocationManager *)manager rangingBeaconsDidFailForRegion:(CLBeaconRegion *)region withError:(NSError *)error {
    NSLog(@"%@", error);
}

+ (CurrentOrderModel *)shareModel {
    static CurrentOrderModel *model = NULL;
    if (model == NULL) model = [CurrentOrderModel new];
    return model;
}
- (Boolean)found { return (_beacon != NULL); }
- (NSNumber *)restaurantNumber { return _beacon.major; }
- (NSNumber *)tableNumber { return _beacon.minor; }


@end
